<?php

namespace frontend\controllers;

use frontend\models\UserCompatibleModel;
use common\models\BaseUser;
use Yii;
use frontend\models\AvatarForm;
use frontend\components\Controller;

/**
 * 用户模块接口
 * PassportController
 */
class ExpandController extends Controller {

    /**
     * Declares the allowed HTTP verbs.
     * Please refer to [[VerbFilter::actions]] on how to declare the allowed verbs.
     * @return array the allowed HTTP verbs.
     */
    protected function verbs() {
        return [
            '*' => ['POST'],
            'userbyname' => ['GET'],
        ];
    }

    /**
     * Declares the allowed HTTP verbs.
     * Please refer to [[VerbFilter::actions]] on how to declare the allowed verbs.
     * @return array the allowed HTTP verbs.
     */
    protected function rules() {
        return [
            [
                'actions' => ['avatar'],
                'allow' => true,
                'roles' => ['@'],
            ],
            [
                'actions' => ['userbyname'],
                'allow' => true,
            ],
        ];
    }

    /**
     * 上传头像
     * @return mixed
     */
    public function actionAvatar() {
        $model = new AvatarForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::error('用户上传头像不能为空');
        }
        return $model->save();
    }

    /**
     * 根据用户名获取用户信息
     * @return mixed
     */
    public function actionUserbyname() {
        $params = Yii::$app->request->getQueryParams();
        if (!isset($params['username'])) {
            parent::error('必须上传用户名');
        }
        return UserCompatibleModel::findByAccount($params['username']);
    }

}
